package cn.doitedu;

import org.roaringbitmap.RoaringBitmap;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

public class Bitmap32SerDeUtil {

    public static byte[] serBitmap(RoaringBitmap bitmap){
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(bout);

        try {
            bitmap.serialize(dout);

            return bout.toByteArray();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    public static RoaringBitmap deSerBitmap(byte[] bmBytes){

        RoaringBitmap bitmap = RoaringBitmap.bitmapOf();
        try {
            bitmap.deserialize(ByteBuffer.wrap(bmBytes));
            return bitmap;

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}
